#!/usr/bin/env bash

source "$rvm_scripts_path/functions/pkg"

requirements_opensuse_lib_available()
{
  zypper search --provides --match-exact "$1" >/dev/null 2>&1 || return $?
}

requirements_opensuse_lib_installed()
{
  rpm -q --whatprovides "$1" >/dev/null || return $?
}

requirements_opensuse_libs_install()
{
  __rvm_try_sudo zypper install -y "$@" || return $?
}

requirements_opensuse_libs_remove()
{
  __rvm_try_sudo zypper rm -y "$@" || return $?
}

requirements_opensuse_update_system()
{
  __rvm_try_sudo zypper --gpg-auto-import-keys refresh ||
  __rvm_try_sudo zypper refresh || # just in case the flag was not supported
  return $?
}

requirements_opensuse_install_custom()
{
  requirements_rvm_pkg_libs_install "$@" || return $?
}

requirements_opensuse_define_libyaml()
{
  requirements_check libyaml-devel
}

requirements_opensuse_define_default()
{
  undesired_check libressl-devel

  requirements_check automake binutils bison bzip2 libtool m4 make patch \
    gdbm-devel glibc-devel libffi-devel libopenssl-devel readline-devel \
    sqlite3-devel zlib-devel

  requirements_${_system_name_lowercase}_define_libyaml

  if
    __rvm_version_compare "${_system_version}" -ge 12.0
    then requirements_check libdb-4_8
    else requirements_check libdb-4_5
  fi
}

requirements_opensuse_define()
{
  case "$1" in
    (rvm)
      requirements_check bash curl patch
      ;;

    (jruby*)
      requirements_check make

      if is_head_or_disable_binary "$1"
      then requirements_check java-devel git
      else requirements_check java
      fi

      case $( jruby_installation_method  "$1" ) in
        ant) requirements_check ant                    ;;
        mvn) requirements_check_custom_after mvn=maven ;;
      esac
      ;;

    (ir*)
      requirements_check mono
      ;;

    (opal)
      requirements_check nodejs
      ;;

    (*-head)
      requirements_check autoconf git
      requirements_opensuse_define "${1%-head}"
      ;;

    (rbx*|rubinius*)
      requirements_opensuse_define_default
      requirements_check_fallback llvm-devel llvm6-devel llvm5-devel llvm4-devel
      requirements_check_fallback clang clang6 clang5 clang4
      ;;

    (*)
      requirements_opensuse_define_default
      requirements_check gcc gcc-c++
      ;;
  esac
}
